﻿using System.Collections;
using UnityEngine;

public class ReplayUnit : MonoBehaviour
{
    CmdListener listener = new CmdListener();
    public int UnitId;
    Vector3 velocity;

    protected Unit unit;

    public virtual void Awake()
    {
        //添加监听：指令类型，处理函数
        listener.Add<PosCmd>(CmdEnum.Pos, DoPosCmd);
        listener.Add<MoveCmd>(CmdEnum.Move, DoMoveCmd);

        UnitMgr.Instance.Register(UnitId, unit);
    }

    private void FixedUpdate()
    {
        transform.position += velocity;
    }

    public void SetUnit(Unit unit)
    {
        this.unit = unit;
    }

    private void DoPosCmd(PosCmd posCmd)
    {
        transform.position = posCmd.Pos.ToVector3();
        transform.localEulerAngles = posCmd.Dir.ToVector3();
    }

    private void DoMoveCmd(MoveCmd cmd)
    {
        velocity = cmd.Velocity.ToVector3();
    }

    public void DoCmd(CmdEnum type, ICommand cmd)
    {
        listener.Do(type, cmd);
        GameMgr.Instance.Record(cmd, unit);
    }
}
